package com.openarena.model.dao;

import com.openarena.model.GameKill;
import com.openarena.model.Player;
import com.openarena.model.Game;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class GameKillsDAO extends InfArenaDAO {

    private static final Logger logger = Logger.getLogger(GameKillsDAO.class.getCanonicalName());

    public GameKillsDAO() {
    }

    public Integer getCountOfGameKillsByGameAndVictim(Game game, Player victim) {
        Session session = null;
        try {
            session = getSession();
            return (Integer) session.createCriteria(GameKill.class)
                    .add(Restrictions.eq("gameId", game.getId()))
                    .add(Restrictions.eq("victimId", victim.getId()))
                    .setProjection(Projections.rowCount())
                    .uniqueResult();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            return null;
        } finally {
            close(session);
        }
    }

    public Integer getCountOfGameKillsByGameAndKiller(Game game, Player killer) {
        Session session = null;
        try {
            session = getSession();
            return (Integer) session.createCriteria(GameKill.class).add(Restrictions.eq("gameId", game.getId())).add(Restrictions.eq("killerId", killer.getId())).setProjection(Projections.rowCount()).uniqueResult();

        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            return null;
        } finally {
            close(session);
        }
    }

    public int getCountOfGameKillsByGameAndKillerAndVictim(Game game, Player killer, Player victim) {
        Session session = null;
        try {
            session = getSession();
            return (Integer) session.createCriteria(GameKill.class)
                    .add(Restrictions.eq("gameId", game.getId()))
                    .add(Restrictions.eq("killerId", killer.getId()))
                    .add(Restrictions.eq("victimId", victim.getId()))
                    .setProjection(Projections.rowCount())
                    .uniqueResult();

        } catch (Exception e) {
            logger.log(Level.SEVERE, "", e);
            return 0;
        } finally {
            close(session);
        }
    }
}
